package net.sk.china.platform.service.impl.upload;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import net.sk.china.platform.response.CloudResponse;
import net.sk.china.platform.service.system.SystemAttachmentService;
import net.sk.china.platform.service.upload.QiNiuService;
import net.sk.china.common.exception.TalkException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.File;

/**
 * QiNiuServiceImpl 接口实现
 * +----------------------------------------------------------------------
 * | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2018-2024 聊来All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * +----------------------------------------------------------------------
 * | @Author: 聊来 <18970881148@qq.com>
 * +----------------------------------------------------------------------
 * | DateTime: 2024/6/2 18:05
 * +----------------------------------------------------------------------
 */

@Service
public class QiNiuServiceImpl implements QiNiuService {

    private static final Logger logger = LoggerFactory.getLogger(QiNiuServiceImpl.class);

    private final SystemAttachmentService systemAttachmentService;

    public QiNiuServiceImpl(SystemAttachmentService systemAttachmentService) {
        this.systemAttachmentService = systemAttachmentService;
    }

    @Override
    public void upload(UploadManager uploadManager, CloudResponse cloud, String upToken, String webPth, String localFile, Integer id) {
        try {
            logger.info("上传文件" + id + "开始：" + localFile);

            File file = new File(localFile);
            if(!file.exists()){
                logger.info("上传文件"+ id + localFile + "不存在：");
                return;
            }

            Response put = uploadManager.put(localFile, webPth, upToken);
            put.close();
            logger.info("上传文件" + id + " -- 结束：" + put.address);
            //更新数据库
            systemAttachmentService.updateCloudType(id, 2);
        } catch (QiniuException ex) {
            throw new TalkException(ex.getMessage());
        }
    }

    @Override
    public void uploadFile(UploadManager uploadManager, CloudResponse cloud, String upToken, String webPth, String localFile, File file) {
        try {
            logger.info("上传文件开始：" + localFile);

//            File file = new File(localFile);
            if(!file.exists()){
                logger.info("上传文件" + localFile + "不存在：");
                return;
            }

//            Response put = uploadManager.put(localFile, webPth, upToken);
            Response put = uploadManager.put(file, webPth, upToken);
            put.close();
            logger.info("上传文件 -- 结束：" + put.address);
        } catch (QiniuException ex) {
            throw new TalkException(ex.getMessage());
        }
    }
}
